
package dyyx.telnetclient;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;


/**
 * 批量自动发消息 ，不需要从控制台输入
 * @author dugang
 *
 */
public final class TelnetClient {

    static final String HOST = System.getProperty("host", "127.0.0.1");
    static final int PORT = Integer.parseInt(System.getProperty("port", "7090"));

    public static void main(String[] args) throws Exception {
       

        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
             .channel(NioSocketChannel.class)
             .handler(new TelnetClientInitializer());

            // Start the connection attempt.
            Channel ch = b.connect(HOST, PORT).sync().channel();
            
            ch.config().setWriteBufferHighWaterMark(10 * 1024 *1024);
            

            
            System.out.println(ch);
            // Read commands from the stdin.
            ChannelFuture lastWriteFuture = null;
            
            // context 增加 evntloopgroup ，按发送顺序返回
            // 使用自定义业务线程池后 ，服务端 执行了 write ，但是 客户端无响应
            lastWriteFuture = ch.writeAndFlush("sleep3000\r\n");
            lastWriteFuture = ch.writeAndFlush("sleep2000\r\n");
            lastWriteFuture = ch.writeAndFlush("sleep1000\r\n");
            lastWriteFuture = ch.writeAndFlush("sleep500\r\n");
            lastWriteFuture = ch.writeAndFlush("sleep400\r\n");

            lastWriteFuture = ch.writeAndFlush("sleep300\r\n");
            lastWriteFuture = ch.writeAndFlush("sleep200\r\n");
            lastWriteFuture = ch.writeAndFlush("sleep100\r\n");
            
            while(true){
            	Thread.sleep(3000);
            }


        } finally {
            group.shutdownGracefully();
        }
    }
}
